<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Exploration extends MY_Controller
{
    public function __construct(){
        parent::__construct();
        $this->load->model("repos/SymbolModel", "SymbolModel");
    }
    
    public function index()
    {
        $data['title'] = 'Exploration tool';
        $data['arrSymbol'] = $this->SymbolModel->getSymbol();
        $this->renderView('exploration/index', $data);
    }

    public function doexplore()
    {
        //check if is post data
        if (!$this->input->post()) {
            show_404();
        }

        //prepare data
        $iSdMean = $this->input->post('sdmean', true);
        if (is_numeric($iSdMean)) {
            if ($iSdMean != 0) {
                if ($iSdMean < 0) {
                    $iSdMean = $iSdMean * -1;
                }
            } else {
                $message = "<span class='error'><strong>SD-MEAN</strong> can not equal 0.</span>";
                $this->load->view('common/script', array('message' => $message));
                return;
            }
        } else {
            $message = "<span class='error'><strong>SD-MEAN</strong> is not a number.</span>";
            $this->load->view('common/script', array('message' => $message));
            return;
        }

        //get pair of company
        $arrRealPost = $this->input->post('cmp', true);
        $arrPost = array();
        foreach ($arrRealPost as $item) {
            if ($item[0] != -1 && $item[1] != -1) {
                array_push($arrPost, $item);
            }
        }
        //detect valid data, at least 1 pair was selected
        if (count($arrPost) < 1) {
            $message = "<span class='error'>Please choose a pair of banks.</span>";
            $this->load->view('common/script', array('message' => $message));
            return;
        }              

        //get date time
        $endDate = new DateTime('now', new DateTimeZone(TIMEZONE));
        $d = $endDate->format('Y-m-d H:i:s');
        $startDate = date('Y-m-d H:i:s', strtotime("$d - 12 months"));
        $startDate = new DateTime($startDate, new DateTimeZone(TIMEZONE));
        $cmpData = null;

        $this->load->library('calculatordata');
        $arrSymbol = $this->SymbolModel->getSymbol();
        $aResult = array();
        $message = '';
        
        foreach ($arrPost as $index => $item) {
            $companyA = $this->getUrlValFromId($arrSymbol, $item[0]);
            $companyB = $this->getUrlValFromId($arrSymbol, $item[1]);
            if ($item[0] != $item[1]) {
                $arrCmpA = $this->SymbolModel->getSymbolData($startDate, $endDate, $item[0]);
                $arrCmpB = $this->SymbolModel->getSymbolData($startDate, $endDate, $item[1]);

                if (empty($arrCmpA) || empty($arrCmpB)) {
                    $message = "<span class='error'>" . NO_RECORD_FOUND . " for <strong>{$companyA}</strong> and <strong>{$companyB}</strong> !</span>";
                } else {
                    $arrCmp = $this->calculatordata->getDataOutPut($arrCmpA, $arrCmpB, $companyA, $companyB);
                    $arrData = array(
                        'tid' => $index,
                        'tdate' => $arrCmp[0][0],
                        'tcomA' => $companyA,
                        'tcomB' => $companyB,
                        'tcomAValue' => $arrCmp[0][1],
                        'tcomBValue' => $arrCmp[0][2],
                        'tratio' => $arrCmp[0][3],
                        'tstd_dev' => $arrCmp[0][4],
                        'tmean_ra' => $arrCmp[0][5],
                        'tds_mean' => $arrCmp[0][6],
                        'tsignal' => $arrCmp[0][7],
                        'tspread' => $arrCmp[0][8]);
                    $cmpData = new CmpDataEntity($arrData);
                    if (count($arrCmp) > 0) {
                        if ($arrCmp[0][6] >= $iSdMean || $arrCmp[0][6] <= $iSdMean * -1) {
                            if ($arrCmp[0][6] >= $iSdMean) {
                                array_push($aResult, $cmpData);
                            }
                            if ($arrCmp[0][6] <= $iSdMean) {
                                array_push($aResult, $cmpData);
                            }
                        } else {
                            if($message!=''){
                                $message .= '<br/>';
                            }
                            $message .= "<span class='error'>" . NO_RECORD_FOUND . " for <strong>{$companyA}</strong> and <strong>{$companyB}</strong> with <strong>SD-MEAN = {$iSdMean}</strong> !</span>";
                        }
                    } else {
                        if($message!=''){
                            $message .= '<br/>';
                        }
                        $message .= "<span class='error'>" . NO_RECORD_FOUND . " for <strong>{$companyA}</strong> and <strong>{$companyB}</strong> !</span>";
                    }
                }
            } else {
                if($message!=''){
                    $message .= '<br/>';
                }
                $message .= "<span class='error'>Bank <strong>$companyA</strong> was the same!</span>";
            }
        }
        
        if($message!=''){
        	$this->load->view('common/script', array('message' => $message));
        } else{
        	$this->load->view('exploration/table', array('data' => $aResult));
        }
    }
}